package com.xidian.zhiancloud.service.myyb;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SysEnterpriseFoundationMapper;
import com.xidian.zhiancloud.dao.SysEnterpriseFoundationMapperExt;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.SysEnterpriseFoundation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 企业基础信息设置
 *
 */
@Service
public class IEnterpriseFoundationService extends ServiceImpl
        <SysEnterpriseFoundationMapper, SysEnterpriseFoundation> implements IService<SysEnterpriseFoundation> {

    @Autowired
    private SysEnterpriseFoundationMapper enterpriseFoundationMapper;

    @Autowired
    private SysEnterpriseFoundationMapperExt enterpriseFoundationMapperExt;

    /**
     * 添加企业认证信息
     * @param enterpriseFoundation
     */
    public void add(SysEnterpriseFoundation enterpriseFoundation) {

        String certificateName=enterpriseFoundation.getCertificateName();
        String certificationAuthority =enterpriseFoundation.getCertificationAuthority();
        Date validity =enterpriseFoundation.getValidity();
        Date reviewReminderDate= enterpriseFoundation.getReviewReminderDate();
        String certificatePhoto=enterpriseFoundation.getCertificatePhoto();
        SysEnterpriseFoundation enterpriseFoundations = new SysEnterpriseFoundation();

        enterpriseFoundations.setEnterpriseName(enterpriseFoundation.getEnterpriseName());
        enterpriseFoundations.setReferred(enterpriseFoundation.getReferred());
        enterpriseFoundations.setIndustry(enterpriseFoundation.getIndustry());
        enterpriseFoundations.setPosition(enterpriseFoundation.getPosition());
        enterpriseFoundations.setPositionDetail(enterpriseFoundation.getPositionDetail());
        enterpriseFoundations.setLongitude(enterpriseFoundation.getLongitude());
        enterpriseFoundations.setLatitude(enterpriseFoundation.getLatitude());

        if (StringUtils.isNotBlank(certificateName)){
            enterpriseFoundations.setCertificateName(certificateName);
        }else{
            enterpriseFoundations.setCertificateName("");
        }

        if (StringUtils.isNotBlank(certificationAuthority)){
            enterpriseFoundations.setCertificationAuthority(certificationAuthority);
        }else{
            enterpriseFoundations.setCertificationAuthority("");
        }

        if (StringUtils.isNotBlank(String.valueOf(validity))){
            enterpriseFoundations.setValidity(validity);
        }else{
            enterpriseFoundations.setValidity(new Date());
        }

        if (StringUtils.isNotBlank(String.valueOf(reviewReminderDate))){
            enterpriseFoundations.setReviewReminderDate(reviewReminderDate);
        }else{
            enterpriseFoundations.setReviewReminderDate(new Date());
        }

        if (StringUtils.isNotBlank(certificatePhoto)){
            enterpriseFoundations.setCertificatePhoto(certificatePhoto);
        }else{
            enterpriseFoundations.setCertificatePhoto("");
        }

        enterpriseFoundations.setCreateTime(new Date());
        enterpriseFoundations.setUpdateTime(new Date());

        int result = enterpriseFoundationMapper.insert(enterpriseFoundations);
        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

    }

    /**
     * 添加公司证件管理
     * @param record
     */
    public void updateByPrimaryKeySelective(SysEnterpriseFoundation record){

        String certificateName=record.getCertificateName();
        String certificationAuthority =record.getCertificationAuthority();
        Date validity =record.getValidity();
        Date reviewReminderDate= record.getReviewReminderDate();
        String certificatePhoto=record.getCertificatePhoto();

        SysEnterpriseFoundation enterpriseFoundation=enterpriseFoundationMapperExt.selectByPrimaryKey(record.getId());

        enterpriseFoundation.setEnterpriseName(enterpriseFoundation.getEnterpriseName());
        enterpriseFoundation.setReferred(enterpriseFoundation.getReferred());
        enterpriseFoundation.setIndustry(enterpriseFoundation.getIndustry());
        enterpriseFoundation.setPosition(enterpriseFoundation.getPosition());
        enterpriseFoundation.setPositionDetail(enterpriseFoundation.getPositionDetail());
        enterpriseFoundation.setLongitude(enterpriseFoundation.getLongitude());
        enterpriseFoundation.setLatitude(enterpriseFoundation.getLatitude());

        if (StringUtils.isNotBlank(certificateName)){
            enterpriseFoundation.setCertificateName(certificateName);
        }else{
            enterpriseFoundation.setCertificateName("");
        }

        if (StringUtils.isNotBlank(certificationAuthority)){
            enterpriseFoundation.setCertificationAuthority(certificationAuthority);
        }else{
            enterpriseFoundation.setCertificationAuthority("");
        }

        if (StringUtils.isNotBlank(String.valueOf(validity))){
            enterpriseFoundation.setValidity(validity);
        }else{
            enterpriseFoundation.setValidity(new Date());
        }

        if (StringUtils.isNotBlank(String.valueOf(reviewReminderDate))){
            enterpriseFoundation.setReviewReminderDate(reviewReminderDate);
        }else{
            enterpriseFoundation.setReviewReminderDate(new Date());
        }

        if (StringUtils.isNotBlank(certificatePhoto)){
            enterpriseFoundation.setCertificatePhoto(certificatePhoto);
        }else{
            enterpriseFoundation.setCertificatePhoto("");
        }

        enterpriseFoundation.setUpdateTime(new Date());

        int result = enterpriseFoundationMapperExt.
                updateByPrimaryKeySelective(enterpriseFoundation);

        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

    }


    /**
     * 添加企业基础信息
     * @param record
     */
    public void updateByPrimaryKeySecurityRegulatorySetting(SysEnterpriseFoundation record){

        SysEnterpriseFoundation enterpriseFoundation=enterpriseFoundationMapperExt
                .selectByPrimaryKey(record.getId());

        String certificateName=enterpriseFoundation.getCertificateName();
        String certificationAuthority =enterpriseFoundation.getCertificationAuthority();
        Date validity =enterpriseFoundation.getValidity();
        Date reviewReminderDate= enterpriseFoundation.getReviewReminderDate();
        String certificatePhoto=enterpriseFoundation.getCertificatePhoto();

        enterpriseFoundation.setEnterpriseName(enterpriseFoundation.getEnterpriseName());
        enterpriseFoundation.setReferred(enterpriseFoundation.getReferred());
        enterpriseFoundation.setIndustry(enterpriseFoundation.getIndustry());
        enterpriseFoundation.setPosition(enterpriseFoundation.getPosition());
        enterpriseFoundation.setPositionDetail(enterpriseFoundation.getPositionDetail());
        enterpriseFoundation.setLongitude(enterpriseFoundation.getLongitude());
        enterpriseFoundation.setLatitude(enterpriseFoundation.getLatitude());

        if (StringUtils.isNotBlank(certificateName)){
            enterpriseFoundation.setCertificateName(certificateName);
        }else{
            enterpriseFoundation.setCertificateName("");
        }

        if (StringUtils.isNotBlank(certificationAuthority)){
            enterpriseFoundation.setCertificationAuthority(certificationAuthority);
        }else{
            enterpriseFoundation.setCertificationAuthority("");
        }

        if (StringUtils.isNotBlank(String.valueOf(validity))){
            enterpriseFoundation.setValidity(validity);
        }else{
            enterpriseFoundation.setValidity(new Date());
        }

        if (StringUtils.isNotBlank(String.valueOf(reviewReminderDate))){
            enterpriseFoundation.setReviewReminderDate(reviewReminderDate);
        }else{
            enterpriseFoundation.setReviewReminderDate(new Date());
        }

        if (StringUtils.isNotBlank(certificatePhoto)){
            enterpriseFoundation.setCertificatePhoto(certificatePhoto);
        }else{
            enterpriseFoundation.setCertificatePhoto("");
        }

        enterpriseFoundation.setIndustry(record.getIndustry());
        enterpriseFoundation.setSecurityRegulatorySetting(record.
                getSecurityRegulatorySetting());

        enterpriseFoundation.setPrincipal(record.getPrincipal());
        enterpriseFoundation.setPrincipalTel(record.getPrincipalTel());
        enterpriseFoundation.setPrincipalPhone(record.getPrincipalPhone());
        enterpriseFoundation.setPrincipalMail(record.getPrincipalMail());

        enterpriseFoundation.setSafePrincipal(record.getSafePrincipal());
        enterpriseFoundation.setSafePrincipalTel(record.getSafePrincipalTel());
        enterpriseFoundation.setSafePrincipalPhone(record.getSafePrincipalPhone());
        enterpriseFoundation.setSafePrincipalMail(record.getSafePrincipalMail());

        enterpriseFoundation.setPractitionerNum(record.getPractitionerNum());
        enterpriseFoundation.setSpecialPractitionerNum(record.getSpecialPractitionerNum());
        enterpriseFoundation.setSafetyProductionManagerNum(record
                .getSafetyProductionManagerNum());
        enterpriseFoundation.setFullTimeSecurityManager(record.getFullTimeSecurityManager());
        enterpriseFoundation.setFullTimeEmergencyManagerNum(record
                .getFullTimeEmergencyManagerNum());
        enterpriseFoundation.setRegisterSecurityEngineerNum(record
                .getRegisterSecurityEngineerNum());

        enterpriseFoundation.setUpdateTime(new Date());

        int result = enterpriseFoundationMapperExt.
                updateByPrimaryKeySelective(enterpriseFoundation);

        if (result == 0) {
            throw new CustomizeException(GeneralResponseEnums.SYSTEM_ERROR);
        }

    }



}
